﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace HRdex_Final.Forms
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");
        string SessionID()
        {
            return Session["EmployeeID"].ToString();

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string CBAmonID = Request.QueryString["CBAmonID"];
            SqlCommand cmdSelect = new SqlCommand("SELECT  EmployeeID, LastName + ', ' + FirstName + ' ' + MiddleName AS Name FROM formEmployees WHERE EmployeeID=" + SessionID(), conn);
            SqlDataAdapter daSelect = new SqlDataAdapter(cmdSelect);
            DataTable dt = new DataTable("dt");
            btnEdit.Visible = false;
            try
            {
                daSelect.Fill(dt);
                lblConfirmationMsg.Visible = false;

                txtEmpNo.Text = dt.Rows[0].ItemArray[0].ToString();
                txtParent.Text = dt.Rows[0].ItemArray[1].ToString();
            }
            catch (Exception)
            {
                
                throw;
            }

            if (CBAmonID == null)
            {
                txtEmpNo.Enabled = false;
                txtParent.Enabled = false;
                btnEdit.Enabled = false;

            }
            else
            {
                select();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string CBAmonID = Request.QueryString["CBAmonID"];

                SqlCommand cmd = new SqlCommand("INSERT INTO CBAmon(StudentNo, StudentLastName, StudentFirstName, CourseID, YearID, Birthdate, OrdinalPositionEnrolled, EmployeeID, ParentName)" +
                    "VALUES (@sNo, @sLname, @sFname, @sCourse, @sYear, @bday, @ordinal, @empID, @parent)", conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);

            if (txtCBAmonID.Text != CBAmonID)
            {

                conn.Open();
                cmd.Parameters.Add("@sNo", SqlDbType.Int).Value = txtStudentNumber.Text;
                cmd.Parameters.Add("@sLname", SqlDbType.VarChar).Value = txtStudentLname.Text;
                cmd.Parameters.Add("@sFname", SqlDbType.VarChar).Value = txtStudentFname.Text;
                cmd.Parameters.Add("@sCourse", SqlDbType.Int).Value = ddlCourse.SelectedValue;
                cmd.Parameters.Add("@sYear", SqlDbType.Int).Value = ddlYear.SelectedValue;
                cmd.Parameters.Add("@bday", SqlDbType.VarChar).Value = txtBday.Text;
                cmd.Parameters.Add("@ordinal", SqlDbType.VarChar).Value = txtOrdinalPos.Text;
                cmd.Parameters.Add("@empID", SqlDbType.Int).Value = txtEmpNo.Text;
                cmd.Parameters.Add("@parent", SqlDbType.VarChar).Value = txtParent.Text;

                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception)
                {
                    
                    
                }

                conn.Close();

                txtStudentFname.Text = "";
                txtStudentLname.Text = "";
                txtStudentNumber.Text = "";
                txtBday.Text = "";
                txtOrdinalPos.Text = "";
                txtParent.Text = "";
                lblConfirmationMsg.Visible = true;
                lblConfirmationMsg.Text = "You successfully add new child";

            }
            
            
        }


        public void Edit()
        {
            string CBAmonID = Request.QueryString["CBAmonID"];
            SqlCommand cmdUpdate = new SqlCommand("UPDATE CBAmon SET StudentNo= '" + txtStudentNumber.Text +
                "', StudentLastName = '" + txtStudentLname.Text +
                "', StudentFirstName= '" + txtStudentFname.Text +
                "', CourseID= " + ddlCourse.SelectedValue +
                ", Birthdate= '" + txtBday.Text +
                "', OrdinalPositionEnrolled = '" + txtOrdinalPos.Text +
                "' WHERE CBAmonID = " + CBAmonID, conn);

            //SqlDataAdapter daUpdate = new SqlDataAdapter(cmdUpdate);
            conn.Open();
            
            int chk = cmdUpdate.ExecuteNonQuery();
            if (chk > 0)
            {
                lblConfirmationMsg.Visible = true;
                lblConfirmationMsg.Text = "You have successfully updated your child's information";
            }
           

            conn.Close();

            txtBday.Enabled = false;
            txtEmpNo.Enabled = false;
            txtOrdinalPos.Enabled = false;
            txtParent.Enabled = false;
            txtStudentFname.Enabled = false;
            txtStudentLname.Enabled = false;
            txtStudentNumber.Enabled = false;
            ddlCourse.Enabled = false;
            ddlYear.Enabled = false;
            btnAdd.Enabled = false;
            btnEdit.Enabled = true;
            
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            txtBday.Enabled = true;
            txtEmpNo.Enabled = true;
            txtOrdinalPos.Enabled = true;
            txtParent.Enabled = true;
            txtStudentFname.Enabled = true;
            txtStudentLname.Enabled = true;
            txtStudentNumber.Enabled = true;
            ddlCourse.Enabled = true;
            ddlYear.Enabled = true;
            btnAdd.Enabled = false;
            btnAdd.Visible = false;
            btnEdit.Enabled = false;
            btnSave.Visible = true;
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtBday.Text = "";
            txtOrdinalPos.Text = "";
            txtStudentFname.Text = "";
            txtStudentLname.Text = "";
            txtStudentNumber.Text = "";
        }
        public void select()
        {
            string CBAmonID = Request.QueryString["CBAmonID"];
            if (CBAmonID != null)
            {
                SqlCommand cmdSelectLoad = new SqlCommand("SELECT CBAmon.CBAmonID, CBAmon.StudentNo, CBAmon.StudentLastName, CBAmon.StudentFirstName, CBAmon.Birthdate, CBAmon.ParentName, CBAmon.OrdinalPositionEnrolled, CBAmon.EmployeeID, CBAmon.CourseID, CBAmon.YearID FROM CBAmon INNER JOIN formEmployees ON CBAmon.EmployeeID = formEmployees.EmployeeID WHERE CBAmon.CBAmonID=" + CBAmonID, conn);
                SqlDataAdapter daSelectLoad = new SqlDataAdapter(cmdSelectLoad);
                DataTable dtSelectLoad = new DataTable("dtSelectLoad");
                try
                {
                    daSelectLoad.Fill(dtSelectLoad);

                    txtCBAmonID.Text = CBAmonID; 
                    txtStudentNumber.Text = dtSelectLoad.Rows[0].ItemArray[1].ToString();
                    txtStudentLname.Text = dtSelectLoad.Rows[0].ItemArray[2].ToString();
                    txtStudentFname.Text = dtSelectLoad.Rows[0].ItemArray[3].ToString();
                    ddlCourse.SelectedValue = dtSelectLoad.Rows[0].ItemArray[8].ToString();
                    ddlYear.SelectedValue = dtSelectLoad.Rows[0].ItemArray[9].ToString();
                    txtBday.Text = dtSelectLoad.Rows[0].ItemArray[4].ToString();
                    txtParent.Text = dtSelectLoad.Rows[0].ItemArray[5].ToString();
                    txtOrdinalPos.Text = dtSelectLoad.Rows[0].ItemArray[6].ToString();

                    txtEmpNo.Text = dtSelectLoad.Rows[0].ItemArray[7].ToString();

                }
                catch (Exception)
                {
                    
                    
                }

                btnAdd.Enabled = false;
                txtBday.Enabled = false;
                txtEmpNo.Enabled = false;
                txtOrdinalPos.Enabled = false;
                txtParent.Enabled = false;
                txtStudentFname.Enabled = false;
                txtStudentLname.Enabled = false;
                txtStudentNumber.Enabled = false;
                ddlCourse.Enabled = false;
                ddlYear.Enabled = false;
                btnEdit.Enabled = true;
                btnClear.Enabled = false;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //Edit();
            string CBAmonID = Request.QueryString["CBAmonID"];
            SqlCommand cmdUpdate = new SqlCommand("UPDATE CBAmon SET StudentNo= '" + txtStudentNumber.Text +
                "', StudentLastName = '" + txtStudentLname.Text +
                "', StudentFirstName= '" + txtStudentFname.Text +
                "', CourseID= " + ddlCourse.SelectedValue +
                ", YearID= " + ddlYear.SelectedValue +
                ", Birthdate= '" + txtBday.Text +
                "', OrdinalPositionEnrolled = '" + txtOrdinalPos.Text +
                "' WHERE CBAmonID = " + CBAmonID, conn);

            //SqlDataAdapter daUpdate = new SqlDataAdapter(cmdUpdate);
            conn.Open();

            cmdUpdate.ExecuteNonQuery();


            conn.Close();

            txtBday.Enabled = false;
            txtEmpNo.Enabled = false;
            txtOrdinalPos.Enabled = false;
            txtParent.Enabled = false;
            txtStudentFname.Enabled = false;
            txtStudentLname.Enabled = false;
            txtStudentNumber.Enabled = false;
            ddlCourse.Enabled = false;
            ddlYear.Enabled = false;
            btnAdd.Enabled = false;
            btnEdit.Enabled = true;

           lblConfirmationMsg.Text = "You successfully edited your child's information";
            
        }
    }
}